# Maintainer: Sergey Popov <popov.sa@gmail.com>

_realname=mlpack
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.1
pkgrel=2
pkgdesc='An intuitive, fast, scalable C++ machine learning library (mingw-w64)'
arch=('any')
url='https://mlpack.org/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-armadillo"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-ensmallen")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cotire"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("https://github.com/mlpack/mlpack/archive/${_realname}-${pkgver}.tar.gz"
        "0001-mingw-w64-cmake.patch"
        "0002-gcc9-openmp.patch")
sha256sums=('161ece5386cea73d69263ecf83a7a1dc5b44527a3d4c174fee231d80ab3e135f'
            'd9261f225fb089cd3717d0daf791e2c9ff705da44effd70bb3cbf76318693b91'
            'c251d698e3262cab1dc18f6c045429ab70bd10632d40b745159f33a6fadda1cc')
_src="mlpack-mlpack-${pkgver}"

prepare() {
  cd "${srcdir}/${_src}"
  patch -Np1 -i "${srcdir}/0001-mingw-w64-cmake.patch"
  patch -Np1 -i "${srcdir}/0002-gcc9-openmp.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_PYTHON_BINDINGS=OFF \
    ../${_src}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR=${pkgdir} -j1 install

  pushd "${pkgdir}${MINGW_PREFIX}" > /dev/null
  install -Dm644 "${srcdir}/${_src}/COPYRIGHT.txt" "share/licenses/${_realname}/COPYRIGHT.txt"
  install -Dm644 "${srcdir}/${_src}/LICENSE.txt" "share/licenses/${_realname}/LICENSE.txt"
  popd > /dev/null
}
